package com.xbongbong.saas.help;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.domain.entity.PaasAppEntity;
import com.xbongbong.paas.domain.entity.ext.OptionalRangeEntity;
import com.xbongbong.paas.enums.DateTimeEnum;
import com.xbongbong.paas.enums.OptionalRangeEnum;
import com.xbongbong.paas.model.PaasAppModel;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.LinkedAttrPoJo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.pro.constant.I18nStringConstant;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainDTO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ContractOutstockEnum;
import com.xbongbong.saas.enums.business.OrderOutStockEnum;
import com.xbongbong.saas.enums.business.ProductEnum;
import com.xbongbong.saas.enums.business.ProductionMaterielOutstockEnum;
import com.xbongbong.saas.enums.business.PurchaseInstockEnum;
import com.xbongbong.saas.enums.product.ContractOutstockProductEnum;
import com.xbongbong.saas.enums.product.PurchaseInstockProductEnum;
import com.xbongbong.saas.enums.product.PurchaseProductEnum;
import com.xbongbong.saas.enums.product.RefundProductEnum;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Component
public class DefaultAttrHelp {

    @Resource
    private PaasAppModel paasAppModel;

    public DefaultAttrPoJo setUserDefaultAttrPoJo(String userId, String userName, String avatar) {
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        List<OptionalRangeEntity> defaultList = new ArrayList<>();
        OptionalRangeEntity optionalRangeEntity = new OptionalRangeEntity();
        optionalRangeEntity.setProperty(OptionalRangeEnum.USER.getValue());
        optionalRangeEntity.setId(userId);
        optionalRangeEntity.setName(userName);
        optionalRangeEntity.setEditable(BasicConstant.ONE);
        optionalRangeEntity.setUserAvatar(avatar);
        defaultList.add(optionalRangeEntity);
        defaultAttrPoJo.setDefaultType(StringConstant.CUSTOM);
        defaultAttrPoJo.setDefaultList(defaultList);
        return defaultAttrPoJo;
    }


    public static DefaultAttrPoJo getDefaultAttrPojo(JSONArray jsonArray) {
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setUseLinkValue(BasicConstant.ONE);
        defaultAttrPoJo.setLinkFormValue(jsonArray);
        return defaultAttrPoJo;
    }

    public static DefaultAttrPoJo setDefaultValue(Object defaultValue) {
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setUseLinkValue(BasicConstant.ONE);
        defaultAttrPoJo.setLinkFormValue(defaultValue);
        return defaultAttrPoJo;
    }
    /**
     * 封装关联id和name到json数组
     *
     * @param dataId
     * @param name
     * @return
     */
    public JSONArray packegeLinkBusinessJsonArray(Long dataId, String name) {
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(StringConstant.SAAS_LINK_BUSINESS_ID, dataId);
        jsonObject.put(StringConstant.SAAS_LINK_BUSINESS_NAME, name);
        jsonArray.add(jsonObject);
        return jsonArray;
    }

    public void addProductLinkAttr(XbbRefTypeEnum xbbRefTypeEnum, List<LinkedAttrPoJo> linkedAttrPoJoList, FormatExplainDTO formatExplainDTO) throws XbbException {
        HandlerExplainDTO handlerExplainDTO = new HandlerExplainDTO();
        Integer distributorMark = formatExplainDTO.getDistributorMark();
        BeanUtil.copyProperties(formatExplainDTO, handlerExplainDTO);
        FieldAttrEntity productField = formatExplainDTO.getProductField();
        List<? extends FieldAttrEntity> subFieldList = productField.getSubForm().getItems();
        Map<String, FieldAttrEntity> subExplainMap = new HashMap<>(subFieldList.size());
        subFieldList.forEach(subfield -> subExplainMap.put(subfield.getAttr(), subfield));
        boolean refundSource = false;
        if (Objects.equals(xbbRefTypeEnum, XbbRefTypeEnum.REFUND)){
            PaasAppEntity paasAppEntity = paasAppModel.getByAlias(XbbRefTypeEnum.JXC.getAlias(), formatExplainDTO.getCorpid());
            if (Objects.nonNull(paasAppEntity)) {
                refundSource = paasAppEntity.getEnable() == 1;
            }
        }
        LinkedAttrPoJo linkedAttrPoJo = new LinkedAttrPoJo();
        BeanUtil.copyProperties(SelectProductEnum.SOURCE_PRICE, linkedAttrPoJo);
        linkedAttrPoJo.setParentAttr("");
        linkedAttrPoJo.setVisible(1);
        linkedAttrPoJo.setDateType(DateTimeEnum.DATETIME.getType());
        int index = linkedAttrPoJoList.size();
        switch (xbbRefTypeEnum) {
            case CONTRACT_OUTSTOCK:
                linkedAttrPoJo.setAttrName(I18nMessageUtil.getMessage(I18nStringConstant.SELL_PRICE));
                if (!ExplainUtil.isFieldNeedHide(subExplainMap.getOrDefault(ContractOutstockProductEnum.SELLING_PRICE.getAttr(), new FieldAttrEntity()), handlerExplainDTO)) {
                    for (int i = 0; i < linkedAttrPoJoList.size(); i++) {
                        LinkedAttrPoJo item = linkedAttrPoJoList.get(i);
                        if (ProductEnum.PRODUCT_IMGS.getAttr().equals(item.getAttr())) {
                            index = i;
                            break;
                        }
                    }
                    linkedAttrPoJoList.add(index, linkedAttrPoJo);
                }
                break;
            case REFUND:
                linkedAttrPoJo.setAttrName(I18nMessageUtil.getMessage(I18nStringConstant.SELL_PRICE));
                if (!ExplainUtil.isFieldNeedHide(subExplainMap.getOrDefault(RefundProductEnum.PRICE.getAttr(), new FieldAttrEntity()), handlerExplainDTO)) {
                    for (int i = 0; i < linkedAttrPoJoList.size(); i++) {
                        LinkedAttrPoJo item = linkedAttrPoJoList.get(i);
                        if (ProductEnum.PRODUCT_IMGS.getAttr().equals(item.getAttr())) {
                            index = i;
                            break;
                        }
                    }
                    linkedAttrPoJoList.add(index, linkedAttrPoJo);
                }
                break;
            case RETURNED_PURCHASE_OUTSTOCK:
                linkedAttrPoJo.setAttrName(RefundProductEnum.PRICE.getAttrName());
                if (!ExplainUtil.isFieldNeedHide(subExplainMap.getOrDefault(SelectProductEnum.COST.getAttr(), new FieldAttrEntity()), handlerExplainDTO)) {
                    for (int i = 0; i < linkedAttrPoJoList.size(); i++) {
                        LinkedAttrPoJo item = linkedAttrPoJoList.get(i);
                        if (ProductEnum.PRODUCT_IMGS.getAttr().equals(item.getAttr())) {
                            index = i;
                            break;
                        }
                    }
                    linkedAttrPoJoList.add(index, linkedAttrPoJo);
                }
                break;
            case REFUND_INSTOCK:
                linkedAttrPoJo.setAttrName(RefundProductEnum.PRICE.getAttrName());
                if (!ExplainUtil.isFieldNeedHide(subExplainMap.getOrDefault(SelectProductEnum.PRICE.getAttr(), new FieldAttrEntity()), handlerExplainDTO)) {
                    for (int i = 0; i < linkedAttrPoJoList.size(); i++) {
                        LinkedAttrPoJo item = linkedAttrPoJoList.get(i);
                        if (ProductEnum.PRODUCT_IMGS.getAttr().equals(item.getAttr())) {
                            index = i;
                            break;
                        }
                    }
                    linkedAttrPoJoList.add(index, linkedAttrPoJo);
                }
                break;
            case PURCHASE_INSTOCK:
                linkedAttrPoJo.setAttrName(PurchaseInstockProductEnum.COST.getAttrName());
                if (!ExplainUtil.isFieldNeedHide(subExplainMap.getOrDefault(SelectProductEnum.COST.getAttr(), new FieldAttrEntity()), handlerExplainDTO)) {
                    for (int i = 0; i < linkedAttrPoJoList.size(); i++) {
                        LinkedAttrPoJo item = linkedAttrPoJoList.get(i);
                        if (ProductEnum.PRODUCT_IMGS.getAttr().equals(item.getAttr())) {
                            index = i;
                            break;
                        }
                    }
                    linkedAttrPoJoList.add(index, linkedAttrPoJo);
                }
                break;
            case RETURNED_PURCHASE:
                linkedAttrPoJo.setAttrName(PurchaseProductEnum.PURCHASE_PRICE.getAttrName());
                if (!ExplainUtil.isFieldNeedHide(subExplainMap.getOrDefault(SelectProductEnum.PRICE.getAttr(), new FieldAttrEntity()), handlerExplainDTO)) {
                    for (int i = 0; i < linkedAttrPoJoList.size(); i++) {
                        LinkedAttrPoJo item = linkedAttrPoJoList.get(i);
                        if (ProductEnum.PRODUCT_IMGS.getAttr().equals(item.getAttr())) {
                            index = i;
                            break;
                        }
                    }
                    linkedAttrPoJoList.add(index, linkedAttrPoJo);
                }
                break;
            case WORK_ORDER_OUTSTOCK:
                linkedAttrPoJo.setAttrName(SelectProductEnum.PRICE.getAttrName());
                if (!ExplainUtil.isFieldNeedHide(subExplainMap.getOrDefault(SelectProductEnum.COST.getAttr(), new FieldAttrEntity()), handlerExplainDTO)) {
                    for (int i = 0; i < linkedAttrPoJoList.size(); i++) {
                        LinkedAttrPoJo item = linkedAttrPoJoList.get(i);
                        if (ProductEnum.PRODUCT_IMGS.getAttr().equals(item.getAttr())) {
                            index = i;
                            break;
                        }
                    }
                    linkedAttrPoJoList.add(index, linkedAttrPoJo);
                }
                break;
            default:
                break;
        }
        // 调整顺序
        if (refundSource || Objects.equals(xbbRefTypeEnum, XbbRefTypeEnum.RETURNED_PURCHASE) || Objects.equals(xbbRefTypeEnum, XbbRefTypeEnum.RETURNED_MATERIEL_INSTOCK)) {
            if (!ExplainUtil.isFieldNeedHide(subExplainMap.getOrDefault(SelectProductEnum.SOURCE_DOC.getAttr(), new FieldAttrEntity()), handlerExplainDTO)) {
                linkedAttrPoJo = new LinkedAttrPoJo();
                BeanUtil.copyProperties(SelectProductEnum.SOURCE_SOURCE_DOC,linkedAttrPoJo);
                linkedAttrPoJo.setParentAttr("");
                linkedAttrPoJo.setVisible(1);
                linkedAttrPoJo.setDateType(DateTimeEnum.DATETIME.getType());
                if (XbbRefTypeEnum.RETURNED_PURCHASE.equals(xbbRefTypeEnum)) {
                    linkedAttrPoJo.setAttrName(PurchaseInstockEnum.SHEET_NO.getAttrName());
                } else if (XbbRefTypeEnum.RETURNED_MATERIEL_INSTOCK.equals(xbbRefTypeEnum)) {
                    linkedAttrPoJo.setAttrName(ProductionMaterielOutstockEnum.SHEET_NO.getAttrName());
                } else if (XbbRefTypeEnum.REFUND.equals(xbbRefTypeEnum) && !Objects.equals(DistributorMarkEnum.DISTRIBUTOR.getCode(),distributorMark)) {
                    linkedAttrPoJo.setAttrName(ContractOutstockEnum.SHEET_NO.getAttrName());
                }else if (XbbRefTypeEnum.REFUND.equals(xbbRefTypeEnum) && Objects.equals(DistributorMarkEnum.DISTRIBUTOR.getCode(),distributorMark)) {
                    linkedAttrPoJo.setAttrName(OrderOutStockEnum.SHEET_NO.getAttrName());
                }
                linkedAttrPoJoList.add(linkedAttrPoJo);
            }
        }
        switch (xbbRefTypeEnum) {
            case OTHER_INSTOCK:
            case RETURNED_MATERIEL_INSTOCK:
            case OTHER_OUTSTOCK:
            case RETURNED_PURCHASE_OUTSTOCK:
            case REFUND_INSTOCK:
            case RETURNED_PURCHASE:
                if (!ExplainUtil.isFieldNeedHide(subExplainMap.getOrDefault(SelectProductEnum.BATCH.getAttr(), new FieldAttrEntity()), handlerExplainDTO)) {
                    linkedAttrPoJo = new LinkedAttrPoJo();
                    BeanUtil.copyProperties(SelectProductEnum.SOURCE_BATCH,linkedAttrPoJo);
                    linkedAttrPoJo.setParentAttr("");
                    linkedAttrPoJo.setVisible(1);
                    linkedAttrPoJo.setDateType(DateTimeEnum.TIME.getType());
                    linkedAttrPoJoList.add(linkedAttrPoJo);
                }
                if (!ExplainUtil.isFieldNeedHide(subExplainMap.getOrDefault(SelectProductEnum.PRODUCE_DATE.getAttr(), new FieldAttrEntity()), handlerExplainDTO)) {
                    linkedAttrPoJo = new LinkedAttrPoJo();
                    BeanUtil.copyProperties(SelectProductEnum.SOURCE_PRODUCE_DATE,linkedAttrPoJo);
                    linkedAttrPoJo.setParentAttr("");
                    linkedAttrPoJo.setVisible(1);
                    linkedAttrPoJo.setDateType(DateTimeEnum.TIME.getType());
                    linkedAttrPoJoList.add(linkedAttrPoJo);
                }
                if (!ExplainUtil.isFieldNeedHide(subExplainMap.getOrDefault(SelectProductEnum.GUARANTEE_PERIOD.getAttr(), new FieldAttrEntity()), handlerExplainDTO)) {
                    linkedAttrPoJo = new LinkedAttrPoJo();
                    BeanUtil.copyProperties(SelectProductEnum.SOURCE_GUARANTEE_PERIOD,linkedAttrPoJo);
                    linkedAttrPoJo.setParentAttr("");
                    linkedAttrPoJo.setVisible(1);
                    linkedAttrPoJo.setDateType(DateTimeEnum.TIME.getType());
                    linkedAttrPoJoList.add(linkedAttrPoJo);
                }
                break;
            case REFUND:
                if (paasAppModel.validateAppEnable(null, XbbRefTypeEnum.JXC.getAlias(), SaasMarkEnum.SAAS.getCode(), formatExplainDTO.getCorpid())){
                    if (!ExplainUtil.isFieldNeedHide(subExplainMap.getOrDefault(SelectProductEnum.BATCH.getAttr(), new FieldAttrEntity()), handlerExplainDTO)) {
                        linkedAttrPoJo = new LinkedAttrPoJo();
                        BeanUtil.copyProperties(SelectProductEnum.SOURCE_BATCH,linkedAttrPoJo);
                        linkedAttrPoJo.setParentAttr("");
                        linkedAttrPoJo.setVisible(1);
                        linkedAttrPoJo.setDateType(DateTimeEnum.TIME.getType());
                        linkedAttrPoJoList.add(linkedAttrPoJo);
                    }
                    if (!ExplainUtil.isFieldNeedHide(subExplainMap.getOrDefault(SelectProductEnum.PRODUCE_DATE.getAttr(), new FieldAttrEntity()), handlerExplainDTO)) {
                        linkedAttrPoJo = new LinkedAttrPoJo();
                        BeanUtil.copyProperties(SelectProductEnum.SOURCE_PRODUCE_DATE,linkedAttrPoJo);
                        linkedAttrPoJo.setParentAttr("");
                        linkedAttrPoJo.setVisible(1);
                        linkedAttrPoJo.setDateType(DateTimeEnum.TIME.getType());
                        linkedAttrPoJoList.add(linkedAttrPoJo);
                    }
                    if (!ExplainUtil.isFieldNeedHide(subExplainMap.getOrDefault(SelectProductEnum.GUARANTEE_PERIOD.getAttr(), new FieldAttrEntity()), handlerExplainDTO)) {
                        linkedAttrPoJo = new LinkedAttrPoJo();
                        BeanUtil.copyProperties(SelectProductEnum.SOURCE_GUARANTEE_PERIOD,linkedAttrPoJo);
                        linkedAttrPoJo.setParentAttr("");
                        linkedAttrPoJo.setVisible(1);
                        linkedAttrPoJo.setDateType(DateTimeEnum.TIME.getType());
                        linkedAttrPoJoList.add(linkedAttrPoJo);
                    }
                }
                break;
            default:
                break;
        }
    }
}
